﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using DevExpress.XtraTreeList.Nodes;

namespace Waker.Cards
{
    public partial class FormDoorPerssionEdit : Base.FormBasePop
    {
        public FormDoorPerssionEdit()
        {
            InitializeComponent();
        }

        private void FormDoorPerssionEdit_Load(object sender, EventArgs e)
        {
            var dt = new BAL.BAL_Device().GetAllDeviceIDAndName();
            ControlUtil.FillImageComboBox(dt, icbDevice, "Device_Name", "Device_ID");
            icbDevice.SelectedIndex = 0;
        }

        private void icbDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (icbDevice.EditValue == null || icbDevice.EditValue == DBNull.Value || string.IsNullOrEmpty(icbDevice.EditValue.ToString()))
                return;
            var dt = new BAL.BAL_Device().GetSubDeviceByDeviceID(icbDevice.EditValue.ToString());
            icbSubDevice.Properties.Items.Clear();
            ControlUtil.FillImageComboBox(dt, icbSubDevice, "LSubDevice_Name", "LSubDevice_ID");
            icbSubDevice.SelectedIndex = 0;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            base.errorPop.ClearErrors();
            if (icbSubDevice.EditValue == DBNull.Value || icbSubDevice.EditValue == null || string.IsNullOrEmpty(icbSubDevice.EditValue.ToString()))
            {
                base.errorPop.SetError(icbSubDevice, "请选择门");
                return;
            }
            BindTreeList();
            simpleButton1.Tag = icbSubDevice.EditValue.ToString();
        }
        private void BindTreeList()
        {
            treeList1.Nodes.Clear();
            var dt = new BAL.BAL_Cards().GetDoorPerssionByLSubDevice_ID(icbSubDevice.EditValue.ToString());
            if (dt == null || dt.Rows.Count < 1)
                return;
            //treeList1.DataSource = dt;
            //treeList1.ParentFieldName = "ParentCode";
            //treeList1.KeyFieldName = "FunCode";
            //treeList1.ExpandAll();
            var dtParent = new BAL.BAL_Cards().GetAllDepartment();
            if (dtParent == null || dtParent.Rows.Count < 1)
                return;
            var indexImageSelect = 23;
            var indexImageState = -1;
            var nodeRoot = treeList1.AppendNode(new object[] { "所有人员" }, 0, -1, indexImageSelect, indexImageState);
            int countAll = 0;
            foreach (DataRow drParent in dtParent.Rows)
            {
                var nodeParent = treeList1.AppendNode(new object[] { drParent["Dept_Name"] }, 0, -1, indexImageSelect, indexImageState);
                //nodeParent.SetValue(this.treeColFunCode, drParent["FunCode"]);
                //nodeParent.SetValue(this.treeColFunName, drParent["FunName"]);
                var drs = dt.Select(string.Format("Dept_ID='{0}'", drParent["Dept_ID"]), "Emp_No ASC");
                if (drs.Length < 1)
                    continue;
                int count = 0;
                foreach (var dr in drs)
                {
                    var node = treeList1.AppendNode(new object[] { dr["Emp_No"], dr["Emp_Name"], dr["Card_No"], dr["Emp_IDCard"] }, nodeParent.Id, -1, indexImageSelect, indexImageSelect);
                    //node.SetValue(this.treeColFunCode, dr["FunCode"]);
                    node.Tag = dr["Emp_ID"];
                    node.SetValue(this.treeColFunName, dr["Emp_Name"]);
                    var isCheck = dr["IsCheck"] == DBNull.Value ? false : Convert.ToBoolean(dr["IsCheck"]);
                    node.Checked = isCheck;
                    //nodeParent.Nodes.Add(nodeParent);
                    if (isCheck)
                        count++;
                }
                if (drs.Length == count)
                {
                    nodeParent.Checked = true;
                    countAll++;
                }
                if (countAll == dtParent.Rows.Count)
                    nodeRoot.Checked = true;
                //treeList1.Nodes.Add(nodeParent);
            }
            treeList1.ExpandAll();
        }
        private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            //从子到父
            if (!e.Node.HasChildren)
            {
                if (!e.Node.ParentNode.HasChildren)
                    return;
                int count = 0;
                foreach (TreeListNode node in e.Node.ParentNode.Nodes)
                {
                    if (node.Checked)
                        count++;
                }
                e.Node.ParentNode.Checked = count == e.Node.ParentNode.Nodes.Count;
                count = 0;
                foreach (TreeListNode node in e.Node.RootNode.Nodes)
                {
                    if (node.Checked)
                        count++;
                }
                e.Node.RootNode.Checked = count == e.Node.RootNode.Nodes.Count;
                return;
            }
            //从父到子
            foreach (TreeListNode nodeParent in e.Node.Nodes)
            {
                nodeParent.Checked = e.Node.Checked;
                if (!nodeParent.HasChildren)
                    continue; ;
                foreach (TreeListNode node in nodeParent.Nodes)
                    node.Checked = e.Node.Checked;
            }
        }
        protected override void FormSave()
        {
            try
            {
                StartWaiting();
                if (simpleButton1.Tag == null)
                    return;
                var listEmpId = new List<string>();
                foreach (TreeListNode nodeParent in treeList1.Nodes[0].Nodes)
                {
                    foreach (TreeListNode node in nodeParent.Nodes)
                    {
                        if (!node.Checked || node.Tag == null || listEmpId.Contains(node.Tag.ToString()))
                        {
                            continue;
                        }
                        listEmpId.Add(node.Tag.ToString());
                    }
                }
                var isOk = new BAL.BAL_Cards().SaveDoorPermission(listEmpId, simpleButton1.Tag.ToString());
                if (isOk)
                {
                    UI.ShowInfo("保存成功");
                    //this.DialogResult = DialogResult.OK; 
                }
                else
                {
                    UI.ShowError("保存失败");
                }
            }
            catch (Exception ex)
            {
                UI.ShowError(ex.Message);
            }
            finally
            {
                EndWaiting();
            }
        }

    }
}
